use utf8;
package iSOCK::Schema::App::Result::Datasheet;

# Created by DBIx::Class::Schema::Loader
# DO NOT MODIFY THE FIRST PART OF THIS FILE

=head1 NAME

iSOCK::Schema::App::Result::Datasheet

=cut

use strict;
use warnings;

use Moose;
use MooseX::NonMoose;
use MooseX::MarkAsMethods autoclean => 1;
extends 'DBIx::Class::Core';

=head1 COMPONENTS LOADED

=over 4

=item * L<DBIx::Class::InflateColumn::DateTime>

=back

=cut

__PACKAGE__->load_components("InflateColumn::DateTime");

=head1 TABLE: C<datasheet>

=cut

__PACKAGE__->table("datasheet");

=head1 ACCESSORS

=head2 id

  data_type: 'varchar'
  is_nullable: 0
  size: 256

=head2 repository_id

  data_type: 'varchar'
  is_foreign_key: 1
  is_nullable: 0
  size: 256

=head2 busy

  data_type: 'boolean'
  is_nullable: 1

=head2 ahead

  data_type: 'boolean'
  is_nullable: 1

=cut

__PACKAGE__->add_columns(
  "id",
  { data_type => "varchar", is_nullable => 0, size => 256 },
  "repository_id",
  { data_type => "varchar", is_foreign_key => 1, is_nullable => 0, size => 256 },
  "busy",
  { data_type => "boolean", is_nullable => 1 },
  "ahead",
  { data_type => "boolean", is_nullable => 1 },
);

=head1 PRIMARY KEY

=over 4

=item * L</id>

=back

=cut

__PACKAGE__->set_primary_key("id");

=head1 RELATIONS

=head2 datasheet_branches

Type: has_many

Related object: L<iSOCK::Schema::App::Result::DatasheetBranch>

=cut

__PACKAGE__->has_many(
  "datasheet_branches",
  "iSOCK::Schema::App::Result::DatasheetBranch",
  { "foreign.owner" => "self.id" },
  { cascade_copy => 0, cascade_delete => 0 },
);

=head2 datasheet_tags

Type: has_many

Related object: L<iSOCK::Schema::App::Result::DatasheetTag>

=cut

__PACKAGE__->has_many(
  "datasheet_tags",
  "iSOCK::Schema::App::Result::DatasheetTag",
  { "foreign.owner" => "self.id" },
  { cascade_copy => 0, cascade_delete => 0 },
);

=head2 repository

Type: belongs_to

Related object: L<iSOCK::Schema::App::Result::Repository>

=cut

__PACKAGE__->belongs_to(
  "repository",
  "iSOCK::Schema::App::Result::Repository",
  { id => "repository_id" },
  { is_deferrable => 1, on_delete => "CASCADE", on_update => "CASCADE" },
);

=head2 user_to_datasheets

Type: has_many

Related object: L<iSOCK::Schema::App::Result::UserToDatasheet>

=cut

__PACKAGE__->has_many(
  "user_to_datasheets",
  "iSOCK::Schema::App::Result::UserToDatasheet",
  { "foreign.datasheet_id" => "self.id" },
  { cascade_copy => 0, cascade_delete => 0 },
);


# Created by DBIx::Class::Schema::Loader v0.07025 @ 2012-06-19 14:05:33
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:t14d1xhHPfGhDoX/fi1FSQ

sub last_tag {
  my ( $self ) = @_;
  
  my @tags = map { $_->name } $self->datasheet_tags;
  
  return unless @tags;
  
  return ( sort @tags )[-1];
}

sub users_loh {
  my ( $self ) = @_;
  
  my ( $u2a, $name );

  for my $u2s_r ( $self->user_to_datasheets ) {
    $u2a->{$u2s_r->user_id}->{$u2s_r->authorization_id} = 1;

    $name->{$u2s_r->user_id} = $u2s_r->user->name;
  }

  my @loh = map { { id => $_, name => $name->{$_}, map { $_ => 1 } sort keys %{$u2a->{$_}} } } sort keys %$u2a;

  return [ map { $_->[1] } sort { $a->[0] cmp $b->[0] } map { [ $_->{name}, $_ ] } @loh ];
}

sub secure {
  my ( $self ) = @_;
  
  if ( my $repository_r = $self->repository ) {
    return $repository_r->secure;
  }
  
  return 0;
}

# You can replace this text with custom code or comments, and it will be preserved on regeneration
__PACKAGE__->meta->make_immutable;
1;
